Security News
Input Validation Vulnerabilities Dominate MITRE's 2024 CWE Top 25 List
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
node-abort-controller
Advanced tools
The node-abort-controller package is an implementation of the AbortController interface, which provides a way to abort one or more Web requests as and when desired. It is commonly used to cancel fetch requests or other asynchronous tasks that can be aborted.
Creating an AbortController instance
This feature allows you to create a new instance of AbortController. You can then use the 'signal' property to pass the abort signal to functions that accept it.
const AbortController = require('node-abort-controller');
const controller = new AbortController();
const { signal } = controller;
Aborting a fetch request
This code sample demonstrates how to use the AbortController to abort an ongoing fetch request. The fetch request is initiated with the abort signal, and if the request is aborted, it will throw an 'AbortError'.
const fetch = require('node-fetch');
const controller = new AbortController();
const { signal } = controller;
fetch('https://example.com', { signal })
.then(response => response.json())
.catch(err => {
if (err.name === 'AbortError') {
console.log('Fetch aborted');
} else {
console.error('Fetch error:', err);
}
});
// Abort the request
controller.abort();
Using AbortController with async/await
This example shows how to use AbortController with async/await syntax. A fetch request is made, and if it is not completed within 5 seconds, the AbortController is used to abort the request.
const fetch = require('node-fetch');
const AbortController = require('node-abort-controller');
async function fetchData(url) {
const controller = new AbortController();
const { signal } = controller;
setTimeout(() => controller.abort(), 5000); // Abort after 5 seconds
try {
const response = await fetch(url, { signal });
return await response.json();
} catch (err) {
if (err.name === 'AbortError') {
console.log('Fetch aborted');
} else {
throw err;
}
}
}
fetchData('https://example.com').catch(console.error);
The 'abort-controller' package is another implementation of the AbortController interface. It provides similar functionality to 'node-abort-controller' and can be used to abort fetch requests or other asynchronous operations.
The 'p-cancelable' package allows you to create cancelable promises. Unlike 'node-abort-controller', which implements the standard AbortController interface, 'p-cancelable' provides a custom API for creating and canceling promises.
The 'cancelable-promise' package is another alternative that provides the ability to cancel promises. It offers a different API compared to 'node-abort-controller' and is not based on the AbortController interface.
AbortController Polyfill for Node.JS based on EventEmitter
import fetch from 'node-fetch'
import AbortController from 'node-abort-controller'
const controller = new AbortController()
const signal = controller.signal
await fetch('https:/www.google.com', { signal })
// Abort after 500ms. Effectively a timeout
setTimeout(() => controller.abort(), 500)
You might not need to! Generally speaking, there are three environments your JavaScript code can run in:
For modern JS APIs, each environment would ideally get a polyfill:
In practice, this is hard. Tooling such as webpack and browserify are great at making sure stuff works out of the box in all environments. But it is quite easy to fail on both points above. In all likelyhood, you end up shipping less than ideal polyfills on platforms that don't even need them. So what is a developer to do? In the case of fetch
and AbortController
I've done the work for you. This is a guide to that work.
If you are building a ...
Congrats! You don't need a library or polyfill at all! Close this tab. Uninstall this package.
Use this package and node-fetch. It is minimally what you need.
Use abort-controller and whatwg-fetch. These are more complete polyfills that will work in all browser environments.
Use abort-controller and cross-fetch. Same as above, except cross-fetch will polyfill correctly in both the browser and node.js
fetch
internally:Use this package and node-fetch. It is the smallest and least opinionated combination for your end users. Application developers targeting Internet Exploer will need to polyfill AbortController
and fetch
on their own. But your library won't be forcing unecessary polyfills on developers who only target modern browsers.
With the above guide in mind, this library has a very specific set of goals:
This is the ideal for library authors who use fetch
and AbortController
internally and target both browser and node developers.
Thank you @mysticatea for https://github.com/mysticatea/abort-controller. It is a fantastic AbortController
polyfill and ideal for many use cases.
FAQs
AbortController for Node based on EventEmitter
The npm package node-abort-controller receives a total of 10,102,943 weekly downloads. As such, node-abort-controller popularity was classified as popular.
We found that node-abort-controller demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.
Research
Security News
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.